<?php



namespace  Ekadmin\Controller;

use \Think\Controller;

use Think\Page;



class QuanController extends CommonController

{

        public  function index()

        {




            $where['uniacid'] = $this->uniacid;

            $obj = M('quan');

            $param = I('get.');
            

            if($param['status']){

                $where['status'] = $param['status'];

            }
            $this->status=intval($param['status']);
            
            if($param['type']){
                $where['type'] = $param['type'];
            }
            $this->type=intval($param['type']);


            $count = $obj->where($where)->count();
            
            $page = new Page($count);
            
            $list = $obj->where($where)->limit($page->firstRow.','.$page->listRows)->order('id desc')->select();

            $this->assign('page',$page->show());
            
            $this->assign('count',$count);

            $this->assign('list',$list);

            $this->display();

        }


        public function  edit()

        {

            $where['uniacid'] = $this->uniacid;
            
            $where["id"]=I("id");
            
            $data = M('quan')->where($where)->find();
            

            $this->assign('data',$data);

            $this->display('form');

        }


        public function  add()

        {

            $this->display('form');

        }



        public function update()

        {

                if(IS_POST){

                    $param = I('post.');
                    
                    
                    
                    if(trim($param["jiner"])==""){
                        ajax_return(1,'抵用金额必须大于0');
                    }
                    

                    if(trim($param["tips"])==""){
                        ajax_return(1,'备注不能为空');
                    }
                    
                    
                    
                    if(intval($param["amount"]<=0)){
                        ajax_return(1,'可领取数量必须大于0');
                    }

                    
                    if($param["type"]==2){
                        if(trim($param["money"])=="" || trim($param["money"])=="0"){
                            ajax_return(1,'满足金额必须大于0');
                        }
                    }
                    

                    $obj = M('quan');
 					$param["endtime"]=strtotime($param["endtime"]);
                     if($param['id']){
                         
                        
                            
                         $obj->save($param);

                    }else{


                         $param['uniacid'] = $this->uniacid;

                            $obj->add($param);

                    }



                    ajax_return(0,'保存成功',U('index'));



                }

        }



        public function del()

        {

            if(IS_POST){

                $param = I('id');

                if(is_array($param)){

                    $map['id'] = array('in',$param);

                }else{

                    $map['id'] = $param;

                }

                M('quan')->where($map)->delete();

                ajax_return(0,'删除成功');

            }

        }

        /**
         *@是否上架 
         */
        public function isstatus(){
            
             if(IS_POST){
            
                $goods = M('quan')->find(I('id'));
            
                $save['status'] = $goods['status'] == 1 ?  2:1;
            
                $save['id'] = I('id');
            
                M('quan')->save($save);
            
                ajax_return(0,'操作成功');
            
            }else{
                ajax_return(1,'非法操作');
            }
            
        }
        
        /**
         *@赠送优惠券列表 
         */
        public function sendquan(){
            
            $where['uniacid'] = $this->uniacid;
            
            $where['type'] = 2;
            
            $obj = M('member_quan');
            
            $param = I('get.');
            
            if($param['start'] && !$param['end']){
            
                $where['addtime'] = array('gt',strtotime($param['start']));
            
            }
            
            if(!$param['start'] && $param['end']){
            
                $where['addtime'] = array('lt',strtotime($param['end']));
            
            }
            
            if($param['start'] && $param['end']){
            
                $where['addtime'] = array('between',[strtotime($param['start']),strtotime($param['end'])]);
            
            }
            
            if($param['status']){
            
                $where['status'] = $param['status'];
            
            }
            $this->status=intval($param['status']);
            
            
            $count = $obj->where($where)->count();
            
            $page = new Page($count);
            
            $list = $obj->where($where)->limit($page->firstRow.','.$page->listRows)->order('id desc')->select();
            
            if($list){
                
                for($i=0;$i<count($list);$i++){
                    
                    if($rs=M("user")->find($list[$i]["uid"])){
                        $list[$i]["name"]=$rs["name"];
                        $list[$i]["mobile"]=$rs["mobile"];
                        $list[$i]["nickname"]=$rs["nickname"];
                        $list[$i]["acv"]=$rs["head"];
                        
                    }
                    
                    if($rs=M("quan")->find($list[$i]["quanid"])){
                        
                        $list[$i]["jiner"]=$rs["jiner"];
                        
                        switch($rs["type"]){
                            case 1:
                                $list[$i]["type1"]="通用";
                                break;
                            case 2:
                                $list[$i]["type1"]="满减";
                                break;
                            case 3:
                                $list[$i]["type1"]="指定商品";
                                break;
                        }
                        
                        $list[$i]["tips"]=$rs["tips"]." 优惠券类型：".$list[$i]["type1"]." 抵用金额：".$list[$i]["jiner"]."元 ID:".$rs["id"];
                        unset($list[$i]["jiner"],$list[$i]["type1"]);
                    }else{
                        switch($list[$i]["quan_type"]){
                            case 1:
                                $list[$i]["type1"]="通用";
                                break;
                            case 2:
                                $list[$i]["type1"]="满减";
                                break;
                            case 3:
                                $list[$i]["type1"]="指定商品";
                                break;
                        }
                        $list[$i]["tips"]="优惠券类型：".$list[$i]["type1"]." 抵用金额：".$list[$i]["jiner"]."元 ID:无";
                    }
                    
                    if($list[$i]["usedtime"]){
                        $list[$i]["usedtime"]=date("Y-m-d H:i:s",$list[$i]["usedtime"]);
                    }else{
                        $list[$i]["usedtime"]="-";
                    }

                }
                
            }
            
            
            $this->quan=M("quan")->where("uniacid=".$this->uniacid)->field("id,tips")->order("id desc")->select();
            
            $this->assign('page',$page->show());
            
            $this->assign('count',$count);
            
            $this->assign('list',$list);
            
            $this->display();
            
            
        }
        
        /**
         *@真正删除已赠送优惠券 
         */
        public function sendquandel(){
            if(IS_POST){
            
                $param = I('id');
            
                if(is_array($param)){
            
                    $map['id'] = array('in',$param);
            
                }else{
            
                    $map['id'] = $param;
            
                }
                
                $map["uniacid"]=$this->uniacid;
            
                M('member_quan')->where($map)->delete();
            
                ajax_return(0,'删除成功');
            
            }
        }

}